import Mock from 'mockjs'

const List = []
const count = 100

const appCodes = ['dcam','dcim','dcom']
const noticeTypes = ['basic','maintenanceExpire','assetExpire', 'workOrder', 'lifeCircle', 'inventory', 'assetLend']
const readStatus = ['read','unRead']
export const users = ['admin', 'tester']
for (let i = 0; i < count; i++) {
  List.push(Mock.mock({
    id: '@increment',
    createTime: '@datetime',
    updateTime: '@datetime',
    'appCode|1': appCodes,
    'noticeType|1': noticeTypes,
    'isRead|1': readStatus,
    'users|1-2':[{
      // 属性 id 是一个自增数，起始值为 1，每次增 1
      'id|+1': 1,
      'user|1': users
    }],
    userId: users,
    title: Mock.Random.ctitle(),
    content: Mock.Random.ctitle( 10, 20 ),
    businessInfo: "{\"s_asstTypeCodes\":\"1,\",\"s_maintenanceEndEnd\":1590313800087}"
  }))
}

export default [
  {
    url: '/notice/api/w/notification/current/unread',
    type: 'get',
    response: () => {
      return {
        status: 'success',
        data: 0
      }
    }
  },
  {
    url: '/notice/api/w/notification/p',
    type: 'get',
    response: config => {
      const page = Number(config.query.page)
      const size = Number(config.query.size)
      const queries = Object.keys(config.query).filter(it => it.indexOf('s_') === 0).map(it => {return {key: it.replace('s_',''), value: config.query[it]}})
      const filteredData = List.filter(it => {
        return queries.length == 0 || (queries.length > 0 && queries.filter(q => it.hasOwnProperty(q.key) && it[q.key].indexOf(q.value) > -1).length === queries.length)
      })
      const data = filteredData.filter((item, index) => index >= page * size && index < (page+1) * size)
      return {
        "status": "success",
        "data": data,
        "pageable": {
          "totalElements": filteredData.length,
          "numberOfElements": data.length,
          "totalPages": Math.ceil(filteredData.length / size),
          "number": page,
          "first": page === 0,
          "last": data.length < size || (data.length === size && (page + 1) * size >= filteredData.length),
          "size": size,
          "fromNumber": page * size,
          "toNumber": page * size + data.length
        }
      }
    }
  }
]

